package com.mxm.mirai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mxm.mirai.dao.Botconfig;
import com.mxm.mirai.dao.Botgroups;
import com.mxm.mirai.mapper.BotgroupsMapper;
import com.mxm.mirai.service.BotgroupsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 漫小猫
 * @since 2020-09-24
 */
@Service
public class BotgroupsServiceImpl extends ServiceImpl<BotgroupsMapper, Botgroups> implements BotgroupsService {

    @Autowired
    BotgroupsMapper botgroupsMapper;

    private Botgroups botgroups = null;

    @Override
    public boolean QueryBotGroupsQQIf(String qq, String qgroup, String task) {
        if (0<botgroupsMapper.selectCount(new QueryWrapper<Botgroups>().eq("qq",qq).eq("qgroup",qgroup).eq("task",task))){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public String QueryBotGroupsState(String qq, String qgroup, String task) {
        botgroups = botgroupsMapper.selectOne(new QueryWrapper<Botgroups>().select("state").eq("qq",qq).eq("qgroup",qgroup).eq("task",task));
        if(botgroups==null){
            return "0";
        }
        return botgroups.getState();
    }

    @Override
    public boolean UpdateBotGroupsQQ(String qq, String qgroup, String task, String state, String master) {
            if (!QueryBotGroupsQQIf(qq, qgroup,task)) {
                botgroups = new Botgroups();
                botgroups.setQq(qq);
                botgroups.setQgroup(qgroup);
                botgroups.setTask(task);
                botgroups.setState(state);
                botgroups.setMaster(master);
                if (botgroupsMapper.insert(botgroups) >= 1) {
                    return true;
                }
                return false;
              }
            UpdateWrapper<Botgroups> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("qq", qq).eq("qgroup", qgroup).eq("task",task).set("state", state).set("master",master);
            if(botgroupsMapper.update(null, updateWrapper)>=0){
                return true;
            }
            return false;
    }

    @Override
    public boolean AbbBotGroupsQQByMaster(String qq, String master) {
        if(0>=botgroupsMapper.selectCount(new QueryWrapper<Botgroups>().eq("master",master).eq("qq",qq).eq("task","200"))){
            botgroups = new Botgroups();
            botgroups.setQq(qq);
            botgroups.setQgroup(null);
            botgroups.setTask("200");
            botgroups.setState(null);
            botgroups.setMaster(master);
            if(botgroupsMapper.insert(botgroups)>=1){
                return true;
            }
            return false;
        }else {
            return true;
        }
    }

    @Override
    public List<Botgroups> QueryBotGroupsByMaster(String master) {
        return botgroupsMapper.selectList(new QueryWrapper<Botgroups>().eq("master",master));
    }

    @Override
    public String QueryBotGroupsQQByMaster(Long qq) {
        List<Botgroups> list = botgroupsMapper.selectList(new QueryWrapper<Botgroups>().eq("qq",String.valueOf(qq)).eq("task","200"));
        if(list.size()==0){
            return "";
        }
        return list.get(0).getMaster();
    }

    /**
     * 删除状态为200的数据
     *
     * @return
     */
    @Override
    public boolean DeleteBotGroupsBytaskAnd200() {
        return botgroupsMapper.delete(new QueryWrapper<Botgroups>().eq("Task","200"))>0?true:false;
    }
}
